/**
 * 
 */
package ar.posti.framework.query.hibernate.filter;

import ar.posti.framework.query.core.Filter;
import ar.posti.framework.query.core.QueryAssembler;

/**
 * @author Posti
 */
public class OrderBy implements Filter {
	private StringBuffer buffer;

	public OrderBy() {
		this.buffer = new StringBuffer();
	}
	
	public OrderBy(String order) {
		this();
		this.ascending(order);
	}

	public OrderBy ascending(String order) {
		return this.addOrder(order);
	}

	public OrderBy descending(String order) {
		return this.addOrder(order + " desc " );
	}

	private OrderBy addOrder(String order) {
		this.buffer.append(buffer.length() == 0? order : "," + order);
		return this;
	}

	public void assemble(QueryAssembler compiler) {
		compiler.orderBy(this.toString());
	}

	@Override
	public String toString() {
		return "order by " + buffer.toString();
	}

	public void assembleCount(QueryAssembler assembler) {
		//nothing to do
	}

	public void assemble(QueryAssembler assembler, Integer firstResult, Integer lastResult) {
		assembler.orderBy(this.toString());
	}
}
